package com.ybzl.gateway;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ybzl.gateway.domain.NoIbeacon;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class T001 {

    private String st1 = "[{\n" +
            "        \"clientIP\": \"10.10.12.104\",\n" +
            "        \"exId\": \"0104\",\n" +
            "        \"rssi\": -100,\n" +
            "        \"tagId\": \"10004\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075158965\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.104\",\n" +
            "        \"exId\": \"0104\",\n" +
            "        \"rssi\": -84,\n" +
            "        \"tagId\": \"40001\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075158645\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.203\",\n" +
            "        \"exId\": \"0203\",\n" +
            "        \"rssi\": -95,\n" +
            "        \"tagId\": \"40001\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075157725\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.222\",\n" +
            "        \"exId\": \"0222\",\n" +
            "        \"rssi\": -85,\n" +
            "        \"tagId\": \"40001\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075158067\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.104\",\n" +
            "        \"exId\": \"0104\",\n" +
            "        \"rssi\": -80,\n" +
            "        \"tagId\": \"40009\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075157413\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.203\",\n" +
            "        \"exId\": \"0203\",\n" +
            "        \"rssi\": -95,\n" +
            "        \"tagId\": \"40009\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075158615\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.222\",\n" +
            "        \"exId\": \"0222\",\n" +
            "        \"rssi\": -78,\n" +
            "        \"tagId\": \"40009\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075158323\n" +
            "    }, {\n" +
            "        \"clientIP\": \"10.10.12.243\",\n" +
            "        \"exId\": \"0243\",\n" +
            "        \"rssi\": -93,\n" +
            "        \"tagId\": \"40009\",\n" +
            "        \"tagPower\": 100,\n" +
            "        \"ts\": 1760075156773\n" +
            "    },\n" +
            "\t{\n" +
            "            \"clientIP\": \"10.10.12.243\",\n" +
            "            \"exId\": \"0243\",\n" +
            "            \"rssi\": -91,\n" +
            "            \"tagId\": \"40009\",\n" +
            "            \"tagPower\": 100,\n" +
            "            \"ts\": 1760075156879\n" +
            "        }\n" +
            "]";

    @Test
    public void t1() {
        System.out.println("提取的一包数据：");
        List<NoIbeacon> noIbeaconList = JSON.parseArray(st1, NoIbeacon.class);
        System.out.println(noIbeaconList);

        System.out.println("根据标签分组：");
        // 根据标签进行分组
        Map<String, List<NoIbeacon>> noIbeaconExIdMap = noIbeaconList.stream().filter(t -> Objects.nonNull(t))
                .collect(Collectors.groupingBy(NoIbeacon::getTagId, TreeMap::new, Collectors.toList()));
        System.out.println(JSON.toJSONString(noIbeaconExIdMap));

        // 【处理每一个标签相关的基站扫描数据包】
        System.out.println("对指定标签下的基站列表以基站进行分组：");
        List<NoIbeacon> tempList = new ArrayList<>();
        for (String tagId : noIbeaconExIdMap.keySet()) {
            // 对指定标签下的基站列表以基站进行分组
            Map<String, List<NoIbeacon>> exIdMap = noIbeaconExIdMap.get(tagId)
                    .stream().collect(Collectors.groupingBy(NoIbeacon::getExId, TreeMap::new, Collectors.toList()));
            System.out.println("标签：" + tagId + " 基站分组结果：" + JSON.toJSONString(exIdMap));

            for (String exId : exIdMap.keySet()) {
                // 在同一个标签下对基站进行分组 获取型号强度最强的一个
                List<NoIbeacon> collect = exIdMap.get(exId).stream().sorted(Comparator.comparing(NoIbeacon::getRssi).reversed()).collect(Collectors.toList());
                tempList.add(collect.get(0));
            }
        }
        System.out.println("标签在这一包数据中保留各个基站的最大记录列表：" + JSON.toJSONString(tempList));

        System.out.println("将List数据按照标签Id分组：");
        Map<String, List<NoIbeacon>> tempListMap = tempList.stream()
                .collect(Collectors.groupingBy(NoIbeacon::getTagId, TreeMap::new, Collectors.toList()));
        System.out.println(JSON.toJSONString(tempListMap));

        // 挨个处理每一个标签的强度情况
        for (String tagId : tempListMap.keySet()) {
            List<NoIbeacon> collect = tempListMap.get(tagId).stream()
                    .sorted(Comparator.comparing(NoIbeacon::getRssi).reversed()).collect(Collectors.toList());
            System.out.println("--<" + JSON.toJSONString(collect));
            NoIbeacon currentNoIbeacon = collect.get(0);
            System.out.println("信号强度最大的记录： tagId " + tagId + " currentNoIbeacon " + JSON.toJSONString(currentNoIbeacon));


        }

    }
}
